package com.springboot.project.common.node;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lby.
 * Date: 2018/4/1.
 * Version: V1.0.
 */
public class ModuleNode implements Comparable{

    //节点id
    private Integer id;

    //父节点
    private Integer parentId;

    //节点名称
    private String name;

    //按钮级别
    private Integer levels;

    //是否为菜单
    private Integer ismenu;

    //按钮的排序
    private Integer order;

    //节点的url
    private String url;

    //点图标
    private String icon;

    //子节点的集合
    private List<ModuleNode> children;

    //get,set方法
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getLevels() {
        return levels;
    }

    public void setLevels(Integer levels) {
        this.levels = levels;
    }

    public Integer getIsmenu() {
        return ismenu;
    }

    public void setIsmenu(Integer ismenu) {
        this.ismenu = ismenu;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public List<ModuleNode> getChildren() {
        return children;
    }

    public void setChildren(List<ModuleNode> children) {
        this.children = children;
    }

    /**
     * 查询子节点时候的临时集合
     */
    private List<ModuleNode> linkedList = new ArrayList<ModuleNode>();

    /**
     * 空构造方法
     */
    public ModuleNode() {
        super();
    }

    /**
     * 构造方法
     */
    public ModuleNode(Integer id, Integer parentId) {
        super();
        this.id = id;
        this.parentId = parentId;
    }

    /**
     * 重写compareTo方法
     */
    @Override
    public int compareTo(Object o) {
        ModuleNode menuNode = (ModuleNode) o;
        Integer order = menuNode.getOrder();
        if (order == null) {
            order = 0;
        }
        return this.order.compareTo(order);
    }

    /**
     * 构建菜单树
     */
    public void buildNodeTree(List<ModuleNode> nodeList) {
        for (ModuleNode treeNode : nodeList) {
            List<ModuleNode> linkedList = treeNode.findChildNodes(nodeList, treeNode.getId());
            if (linkedList.size() > 0) {
                treeNode.setChildren(linkedList);
            }
        }
    }

    /**
     * 查询子节点的集合
     */
    public List<ModuleNode> findChildNodes(List<ModuleNode> nodeList, Integer parentId) {
        if (nodeList == null && parentId == null)
            return null;
        for (Iterator<ModuleNode> iterator = nodeList.iterator(); iterator.hasNext(); ) {
            ModuleNode node = (ModuleNode) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (node.getParentId() != 0 && parentId.equals(node.getParentId())) {
                recursionFn(nodeList, node, parentId);
            }
        }
        return linkedList;
    }

    /**
     * 遍历一个节点的子节点
     */
    public void recursionFn(List<ModuleNode> nodeList, ModuleNode node, Integer pId) {
        List<ModuleNode> childList = getChildList(nodeList, node);// 得到子节点列表
        if (childList.size() > 0) {// 判断是否有子节点
            if (node.getParentId().equals(pId)) {
                linkedList.add(node);
            }
            Iterator<ModuleNode> it = childList.iterator();
            while (it.hasNext()) {
                ModuleNode n = (ModuleNode) it.next();
                recursionFn(nodeList, n, pId);
            }
        } else {
            if (node.getParentId().equals(pId)) {
                linkedList.add(node);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ModuleNode> getChildList(List<ModuleNode> list, ModuleNode node) {
        List<ModuleNode> nodeList = new ArrayList<ModuleNode>();
        Iterator<ModuleNode> it = list.iterator();
        while (it.hasNext()) {
            ModuleNode n = (ModuleNode) it.next();
            if (n.getParentId().equals(node.getId())) {
                nodeList.add(n);
            }
        }
        return nodeList;
    }

    /**
     * 清除掉按钮级别的资源
     */
    public static List<ModuleNode> clearBtn(List<ModuleNode> nodes) {
        ArrayList<ModuleNode> noBtns = new ArrayList<ModuleNode>();
        for (ModuleNode node : nodes) {
            if(node.getIsmenu() == 1 || node.getIsmenu() == 2){
                noBtns.add(node);
            }
        }
        return noBtns;
    }

    /**
     * 清除所有二级菜单
     */
    public static List<ModuleNode> clearLevelTwo(List<ModuleNode> nodes) {
        ArrayList<ModuleNode> results = new ArrayList<ModuleNode>();
        for (ModuleNode node : nodes) {
            Integer levels = node.getLevels();
            if (levels.equals(1)) {
                results.add(node);
            }
        }
        return results;
    }

    /**
     * 构建菜单列表
     */
    public static List<ModuleNode> buildTitle(List<ModuleNode> nodes) {

        List<ModuleNode> clearBtn = clearBtn(nodes);

        new ModuleNode().buildNodeTree(clearBtn);

        List<ModuleNode> menuNodes = clearLevelTwo(clearBtn);

        //对菜单排序
        Collections.sort(menuNodes);

        //对菜单的子菜单进行排序
        for (ModuleNode menuNode : menuNodes) {
            if (menuNode.getChildren() != null && menuNode.getChildren().size() > 0) {
                Collections.sort(menuNode.getChildren());
            }
        }
        return menuNodes;
    }

}
